<?
	define('IN_SITE', true);
	include("common.php");
	
	$base_url= 'category.php';
	$dep_edit_page= "template/cat_add.tpl";
	$dep_body_page= "template/ypcat_list.tpl";
	
	cat_list();
//----------------------------List-------------------------
function cat_list(){
	global $db, $template,$dep_body_page;
	
	$id	= isset($_GET["id"]) ? htmlspecialchars($_GET["id"]) : ''; 
	
	$sql="select * from vb_ypcategory where cat_active=1 and cat_Name!='Others' order by cat_Name";
	if(!$result=$db->sql_query($sql)){
		message_die("Couldn't run the sql query!!!","",__LINE__,__FILE__,$sql);
	}
	$cat_count1=$db->sql_numrows($result);
	$cat_data1=$db->sql_fetchrowset($result);	
	$db->sql_freeresult($result);
	
	$sql="select * from vb_ypcategory where cat_active=1 and cat_Name='Others' order by cat_Sub";
	if(!$result=$db->sql_query($sql)){
		message_die("Couldn't run the sql query!!!","",__LINE__,__FILE__,$sql);
	}
	$cat_count2=$db->sql_numrows($result);
	$cat_data2=$db->sql_fetchrowset($result);	
	$db->sql_freeresult($result);
	
	$cat_count = intval($cat_count1)+intval($cat_count2);
	$cat_data  = array_merge($cat_data1,$cat_data2);
	
	$str = "";
	$x = 0 ;
	$fg2 = false;
	$fg3 = false;
	$fg4 = false;
	$str1 = "var arr1 = [";
	$str2 = "var arr2 = [";
	$str3 = "var arr3 = [";
	$str4 = "var arr4 = [";
		
	for($i=0;$i<$cat_count;$i++){
	
		$cat_data[$i]['cat_Name'] = ereg_replace("'","\'",$cat_data[$i]['cat_Name']);
		$cat_data[$i]['cat_Name'] = html_entity_decode($cat_data[$i]['cat_Name']);
	
		if ($cat_data[$i]['cat_Level']==1){
			$str1 = $str1 . "'" . $cat_data[$i]['cat_Name']."','".$cat_data[$i]['catID']."',";
		}elseif($cat_data[$i]['cat_Level']==2)	{
			if($x != $cat_data[$i]['cat_Sub']){
					if(!$fg2){
						$str2 = $str2 . "['" . $cat_data[$i]['cat_Sub']."',";
						$fg2 = true;
					}else{
						$str2 = substr($str2,0,strlen($str2)-1)."],['".$cat_data[$i]['cat_Sub']."',";
					}
					$x = $cat_data[$i]['cat_Sub'] ;
				}
				$str2 = $str2 . "'" .$cat_data[$i]['cat_Name']  . "','" . $cat_data[$i]['catID'] . "',";
		}elseif($cat_data[$i]['cat_Level']==3){
				if($x != $cat_data[$i]['cat_Sub']){
					if(!$fg3){
						$str3= $str3 . "['" . $cat_data[$i]['cat_Sub'] . "',";
						$fg3 = true;
					}else{
						$str3 = substr($str3,0,strlen($str3)-1) . "],['" . $cat_data[$i]['cat_Sub'] . "',";
					}
					$x = $cat_data[$i]['cat_Sub'] ;
				}
				$str3 = $str3 . "'" .$cat_data[$i]['cat_Name']  . "','" . $cat_data[$i]['catID'] . "',";
		}elseif($cat_data[$i]['cat_Level']==4){
					if($x != $cat_data[$i]['cat_Sub']){
						if(!$fg4){
							$str4 = $str4 . "['" . $cat_data[$i]['cat_Sub'] . "',";
							$fg4 = true;
						}else{
							$str4 = substr($str4,0,strlen($str4)-1) . "],['" . $cat_data[$i]['cat_Sub'] . "',";
						}
						$x = $cat_data[$i]['cat_Sub'] ;
					}
					$str4 = $str4 . "'" .$cat_data[$i]['cat_Name']  . "','" . $cat_data[$i]['catID'] . "',";
		}		
	} //Ef
	
	if($str1 == "var arr1 = ["){
		$str1 = "var arr1 = []";		
	}
	if($str2 == "var arr2 = ["){
	   $str2="var arr2 = [[]";
	}   
	if($str3 == "var arr3 = ["){
	   $str3="var arr3 = [[]";
	}  
	if($str4 == "var arr4 = ["){
	   $str4="var arr4 = [[]";
	}
	   	 
	$var= substr($str1,0,strlen($str1)-1) . "]\n " . substr($str2,0,strlen($str2)-1) . "]]\n " . substr($str3,0,strlen($str3)-1) . "]]\n " . substr($str4,0,strlen($str4)-1) . "]]\n " ;
	
	//ob_start();  
	if($id){
		showCategory($id,'en');
	}
	$template->assign_vars(array('variable' =>$var));
	$template->set_filenames(array('body' =>$dep_body_page));
	$template->pparse('body');		
	//ob_end_flush();
  	//ob_clean();
}
?>